package com.example.demo.service;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;


import java.text.SimpleDateFormat;
import java.util.*;

/**
 *   日志测试
 */

@Slf4j
public class GenLogData {

    /**
     * 二、注解方式实现日志
     */

   public static void test2(){
        String project=getProject();
        Long randomDate=randomDate("2018-08-13","2018-08-17");
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String sd = sdf.format(new Date(randomDate));   // 时间戳转换成时间
        String info=getInfo();

        JSONObject json=new JSONObject();
        String agent=getUserAgent();
        String method=getMethod();
        String requestMethod=getRequestMethod();
        json.put("userAgent",agent);
        json.put("method",method);
        json.put("ip",getRandomIp());
        json.put("requestMethod",requestMethod);
        UUID uuid = UUID.randomUUID();
        json.put("userId",uuid.toString());
        String url=getUrl();
        json.put("url",url);
        int i = (int)(Math.random()*400+1);
        json.put("consume",i);
       Integer responseCode=getCode();
        json.put("responseCode",responseCode);
        String data=json.toString();
        String logData=project+"["+sd+"]"+info+data;
        log.info(logData);
    }


    /**
     * @Author 蔡高情
     * @Description  随机获取项目
     * @Date 15:47 2018/8/15 0015
     * @Param []
     * @return java.lang.String
     **/
    public static String getProject(){
        List<Object> list =new ArrayList<Object>();
        list.add("[log]");
        list.add("[web]");
        list.add("[scop]");
        list.add("[scpc]");
        list.add("[mam]");
        String project=(String)getRandomList(list);
        return project;
    }
    /**
     * @Author 蔡高情
     * @Description  随机返回状态码
     * @Date 15:47 2018/8/15 0015
     * @Param []
     * @return java.lang.String
     **/
    public static Integer getCode(){
        List<Object> list =new ArrayList<Object>();
        list.add(200);
        list.add(400);
        list.add(500);
        list.add(301);
        list.add(302);
        Integer project=(Integer)getRandomList(list);
        return project;
    }


    /**
     * @Author 蔡高情
     * @Description  随机获取项目
     * @Date 15:47 2018/8/15 0015
     * @Param []
     * @return java.lang.String
     **/
    public static String getUrl(){
        List<Object> list =new ArrayList<Object>();
        list.add("/home/web");
        list.add("/api/test/getIp");
        list.add("/api/getGc");
        list.add("/api/getName");
        list.add("/api/getSex");
        String project=(String)getRandomList(list);
        return project;
    }


    /**
     * @Author 蔡高情
     * @Description  随机方法
     * @Date 15:47 2018/8/15 0015
     * @Param []
     * @return java.lang.String
     **/
    public static String getMethod(){
        List<Object> list =new ArrayList<Object>();
        list.add("com.example.demo.controller.ApiController.shotLog");
        list.add("com.example.demo.controller.TestController.getId");
        list.add("com.example.demo.controller.ApiController.getUrl");
        list.add("com.example.demo.controller.ApiController.getMethod");
        list.add("com.example.demo.controller.ApiController.testLog");
        String method=(String)getRandomList(list);
        return method;
    }



    /**
     * @Author 蔡高情
     * @Description  随机获取日志级别
     * @Date 15:47 2018/8/15 0015
     * @Param []
     * @return java.lang.String
     **/
    public static  String getInfo(){
        List<Object> list =new ArrayList<Object>();
        list.add("[INFO]");
        list.add("[ERROR]");
        list.add("[WARN]");
        list.add("[Console]");
        String project=(String)getRandomList(list);
        return project;
    }

    public static String getRequestMethod(){
        List<Object> list =new ArrayList<Object>();
        list.add("GET");
        list.add("POST");
        list.add("HEAD");
        String requestMethod=(String)getRandomList(list);
        return requestMethod;
    }


    public static String getRandomIp(){
        //ip范围
        int[][] range = {{607649792,608174079},//36.56.0.0-36.63.255.255
                {1038614528,1039007743},//61.232.0.0-61.237.255.255
                {1783627776,1784676351},//106.80.0.0-106.95.255.255
                {2035023872,2035154943},//121.76.0.0-121.77.255.255
                {2078801920,2079064063},//123.232.0.0-123.235.255.255
                {-1950089216,-1948778497},//139.196.0.0-139.215.255.255
                {-1425539072,-1425014785},//171.8.0.0-171.15.255.255
                {-1236271104,-1235419137},//182.80.0.0-182.92.255.255
                {-770113536,-768606209},//210.25.0.0-210.47.255.255
                {-569376768,-564133889}, //222.16.0.0-222.95.255.255
        };

        Random rdint = new Random();
        int index = rdint.nextInt(10);
        String ip = num2ip(range[index][0]+new Random().nextInt(range[index][1]-range[index][0]));
        return ip;
    }

    public static String num2ip(int ip) {
        int [] b=new int[4] ;
        String x = "";

        b[0] = (int)((ip >> 24) & 0xff);
        b[1] = (int)((ip >> 16) & 0xff);
        b[2] = (int)((ip >> 8) & 0xff);
        b[3] = (int)(ip & 0xff);
        x=Integer.toString(b[0])+"."+Integer.toString(b[1])+"."+Integer.toString(b[2])+"."+Integer.toString(b[3]);

        return x;
    }


    public  static String getUserAgent(){
        List<Object> list =new ArrayList<>();
        list.add("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1");

        list.add("Mozilla/5.0 (X11; CrOS i686 2268.111.0) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11");
        list.add("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.6 (KHTML, like Gecko) Chrome/20.0.1092.0 Safari/536.6");
        list.add("Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.6 (KHTML, like Gecko) Chrome/20.0.1090.0 Safari/536.6");
        list.add("Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/19.77.34.5 Safari/537.1");
        list.add("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.9 Safari/536.5");
        list.add("Mozilla/5.0 (Windows NT 6.0) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.36 Safari/536.5");
        list.add("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3");
        list.add("Mozilla/5.0 (Windows NT 5.1) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3");
        list.add("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_0) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3");
        list.add("Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1062.0 Safari/536.3");
        list.add("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1062.0 Safari/536.3");
        list.add("Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3");
        list.add("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3");
        list.add("Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3");
        list.add("Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.0 Safari/536.3");
        list.add("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.24 (KHTML, like Gecko) Chrome/19.0.1055.1 Safari/535.24");
        list.add("Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/535.24 (KHTML, like Gecko) Chrome/19.0.1055.1 Safari/535.2");
        String userAgent= (String)getRandomList(list);
        return userAgent;
    }




    public static Object getRandomList(List<Object> list){
        Random random = new Random();
        int n = random.nextInt(list.size());
        return list.get(n);
    }


    /**
     * 生成随机时间
     * @param beginDate
     * @param endDate
     * @return
     */
    private static long randomDate(String beginDate, String  endDate ){
        long date=0L;
        try {

            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

            Date start = format.parse(beginDate);//构造开始日期

            Date end = format.parse(endDate);//构造结束日期
            if(start.getTime() >= end.getTime()){
                return 0L;
            }
            date = random(start.getTime(),end.getTime());
        } catch (Exception e) {
            e.printStackTrace();
        }

        return date;

    }

    private static long random(long begin,long end){

        long rtn = begin + (long)(Math.random() * (end - begin));

        //如果返回的是开始时间和结束时间，则递归调用本函数查找随机值

        if(rtn == begin || rtn == end){
            return random(begin,end);
        }
        return rtn;
    }

}